<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    /* .adBox{
            width: 300px;
            height: 400px;
            background-color: #6ff;
            position: fixed;
            right: 5px;
            bottom: 5px;
        }
        .adBox .close{
            position: absolute;
            right: 0;
            top: 0;
            background-color: red;
            color: #fff;
            padding: 2px;
        } */
  </style>
</head>

<body>
  <!-- <div class="adBox">
        <div class="tips">距离广告关闭还有10秒</div>
        <div class="close">X</div>
    </div> -->
</body>
<script>
  //  延迟5s 创建小广告 => 倒计时10秒后结束 => 在开启下一轮(延迟5s 显示小广告 => 倒计时10秒后结束)
  var delayTimer = null;
  var timer = null;

  loop(5000);

  function loop(delay) {
    delayTimer = setTimeout(function () {
      // 创建小广告
      var adBox = document.createElement("div");
      adBox.setAttribute("class", "adBox");
      // css文本
      adBox.style.cssText = `width: 300px;
                                height: 400px;
                                background-color: #6ff;
                                position: fixed;
                                right: 5px;
                                bottom: 5px;`

      var tips = document.createElement("div");
      tips.setAttribute("class", "tips");
      tips.innerText = "距离广告关闭还有10秒";
      adBox.appendChild(tips);

      var closeDiv = document.createElement("div");
      closeDiv.setAttribute("class", "close");
      closeDiv.innerText = "X";
      adBox.appendChild(closeDiv);
      closeDiv.style.cssText = `position: absolute;
                    right: 0;
                    top: 0;
                    background-color: red;
                    color: #fff;
                    padding: 2px;`

      closeDiv.onclick = function () {
        adBox.remove();   //删除广告
        clearTimeout(delayTimer);  // 清除延时器
        clearInterval(timer);   //清除计时器
        loop(5000);   //开启下一个小广告
        window.open("https://www.baidu.com");  // 跳转
      }

      document.documentElement.appendChild(adBox);

      // 倒计时10 
      var num = 10;
      tips.innerText = `距离广告关闭还有${num}秒`;
      timer = setInterval(function () {
        num--;
        tips.innerText = `距离广告关闭还有${num}秒`;
        if (num == 0) {
          adBox.remove();   //倒计时结束  删除小广告
          loop(delay);
        }
      }, 1000);
    }, delay)
  }






</script>

</html>